<template>
  <div class="container">
      <div style="width: 100%; padding: 10px 20px; font-size: 24px; background-color: rgba(0,0,0,.3); color: white; position: absolute; top: 0">智慧康云医联平台</div>
      <div style="width: 500px;height: 450px; padding: 30px; background-color: rgba(255, 255, 255, .8); border-radius: 5px;">
        <div style="text-align: center; font-size: 25px; margin-bottom: 20px; color: #333">欢迎使用</div>
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password  v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item>
            <div style="display: flex; align-items: center">
              <div style="flex: 1">
                <el-input size="medium" v-model="code" placeholder="请输入验证码"></el-input>
              </div>
              <Identify @click.native="refreshCode" :identify-code="identifyCode"/>
            </div>
          </el-form-item>
          <el-form-item prop="role">
            <el-select v-model="form.role" style="width: 100%" @change="handleRoleChange">
              <el-option label="用户" value="USER"></el-option>
              <el-option label="医生" value="DOCTOR"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%; background-color: #333; border-color: #333; color: white" @click="login">登 录</el-button>
          </el-form-item>
          <div style="display: flex; align-items: center">
            <div style="flex: 2"><a href="/loginAdmin">后台登录</a></div>
            <div style="flex: 3; text-align: right">
              还没有账号？请 <a style="font-size: 16px" href="/register">注册</a> 用户账号
            </div>
          </div>
        </el-form>
      </div>
    </div>
</template>

<script>
import Identify from "@/components/Identify";
export default {
  name: "Login",
  components: {Identify},
  data() {
    return {
      form: { role: 'USER' },
      code: '',
      // 图片验证码
      identifyCode: '',
      // 验证码规则
      identifyCodes: '3456789ABCDEFGHGKMNPQRSTUVWXY',
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
        ]
      }
    }
  },
  created() {
    this.refreshCode()
  },
  methods: {
    handleRoleChange(value) {
      if (value === 'USER') {
        this.info('user');
      } else if (value === 'DOCTOR') {
        this.info('doctor');
      }
    },
    info(user) {
      alert('体验账号：' + user + '，密码：' + user);
    },
    login() {
      if (this.code.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.warning("验证码错误")
        this.refreshCode()
        this.code = ''
        return
      }
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/login', this.form).then(res => {
            if (res.code === '200') {
              localStorage.setItem("xm-user", JSON.stringify(res.data))  // 存储用户数据
              this.$router.push('/front/home')  // 跳转主页
              this.$message.success('登录成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    // 切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    // 生成随机验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
      }
    },
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("../assets/imgs/其他/bg.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}

a {
  color: #2a60c9;
}
</style>
